import { login, forgetPwd, getVerifyCode, getUserInfo, register, getRegisterCode } from '@/api/login'
import { getUserId, setUserId, removeUserId, getType, setType, removeType } from '@/utils/auth'

const state = {
    mobile: '',
    password: '',
    type: '',
    user_id: getUserId(),
    type: getType() || '1',
    userInfo: null
    
}
const mutations = {
    SET_USERID (state, id) {
        state.user_id = id
    },
    SET_USERINFO (state, payload) {
        state.userInfo = Object.assign({}, state.userInfo, payload.userInfo )
    }
}

const actions = {
    // 用户登录
    login ({commit}, payload) {
        const { type, mobile, password } = payload;
        return new Promise((resolve, reject) => {
            login(type, mobile, password).then(response => {
                const data = response.data;
                commit('SET_USERID', data.user_id);
                setUserId(data.user_id)
                setType(data.type)
                resolve();
            }).catch(error => {
                reject(error)
            })
        })   
    },
    //忘记密码
    forgetPwd ({commit}, payload) {
        const { type, mobile, password } = payload;
        return new Promise((resolve, reject) => {
            forgetPwd(type, mobile, password).then(response => {
                resolve(response);
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 获取验证码
    getVerifyCode ({commit, state}, payload) {
        const { mobile } = payload
        return new Promise((resolve, reject) => {
            getVerifyCode(mobile, state.type).then(response => {
                const data = response.data;
                debugger
                console.log(data);
                // commit('LOGIN')
                resolve();
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 获取个人信息
    getUserInfo ({ commit, state }) {
        return new Promise ((resolve, reject) => {
            getUserInfo(state.type, state.user_id).then(response => {
                const data = response.data;
                commit('SET_USERINFO', {userInfo: data.user_info})
                resolve();
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 学员注册
    register ({ commit, state }, payload) {
        const { mobile, password } = payload
        debugger
        return new Promise((resolve, reject) => {
            register(mobile, password).then(response => {
                debugger;
                console.log(response)
                resolve()
            }).catch(error => {
                reject(error)
            } )
        })
    },
    // 注册获取验证码
    getRegisterCode ({ commit, state }, payload) {
        const { mobile } = payload
        return new Promise((resolve, reject) => {
            getRegisterCode(mobile, state.type).then(response => {
                const data = response.data;
                debugger;
                resolve()
            }).catch(err => {
                reject(err)
            })
        })
    }
}

export default {
    state,
    mutations,
    actions
}